//
//  YHChatMessage.m
//  YHChat
//
//  Created by 银河 on 2019/2/17.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHChatMessage.h"

//#import "TIMCustomElem+YHChat.h"

@interface YHChatMessage ()
@property (nonatomic, strong) TIMMessage *sdk_message;
@end


@implementation YHChatMessage

+ (instancetype)msgWith:(TIMMessage *)msg{
    YHChatMessageType messageType = YHChatMessageType_Unknown;
    if (msg.elemCount > 1) {
        messageType = YHChatMessageType_Multi;
    } else {
        TIMElem *elem = [msg getElem:0];//取的是第一个
        Class eleCls = [elem class];
        if (eleCls == [TIMTextElem class])
        {
            messageType = YHChatMessageType_Text;
        }
        else if (eleCls == [TIMImageElem class])
        {
            messageType = YHChatMessageType_Image;
        }
        else if (eleCls == [TIMFaceElem class])
        {
            messageType = YHChatMessageType_Face;
        }
        else if (eleCls == [TIMSoundElem class])
        {
            messageType = YHChatMessageType_Sound;
        }
        else if (eleCls == [TIMCustomElem class])
        {
            messageType = YHChatMessageType_Custom;
        }
        else if (eleCls == [TIMGroupTipsElem class])
        {
            messageType = YHChatMessageType_GroupTip;
        }
        else if (eleCls == [TIMGroupSystemElem class])
        {
            messageType = YHChatMessageType_SystemTip;
        }
    }
    return [[YHChatMessage alloc] initWithMsg:msg messageType:messageType];
}

- (instancetype)initWithMsg:(TIMMessage *)msg messageType:(YHChatMessageType)messageType
{
    self = [super init];
    if (self) {
        self.sdk_message = msg;
        self.messageType = messageType;
        self.messageStatus = YHChatMessageStatus_Init;
    }
    return self;
}


+ (instancetype)msgWithText:(NSString *)text{
    TIMTextElem *elem = [[TIMTextElem alloc] init];
    elem.text = text;
    TIMMessage *msg = [[TIMMessage alloc] init];
    [msg addElem:elem];
    
    return [[YHChatMessage alloc] initWithMsg:msg messageType:YHChatMessageType_Text];
}

+ (instancetype)msgWithImageWithPath:(NSString *)path{
    TIMImageElem *elem = [[TIMImageElem alloc] init];
    elem.path = path;
    TIMMessage *msg = [[TIMMessage alloc] init];
    [msg addElem:elem];
    return [[YHChatMessage alloc] initWithMsg:msg messageType:YHChatMessageType_Image];
}

+ (instancetype)msgWithDate:(NSDate *)date{
    TIMCustomElem *elem = [[TIMCustomElem alloc] init];
//    [elem setTimeDate:date];
    
    TIMMessage *msg = [[TIMMessage alloc] init];
    [msg addElem:elem];
    
    return [[YHChatMessage alloc] initWithMsg:msg messageType:YHChatMessageType_TimeTip];
}

+ (instancetype)msgWithSoundWithPath:(NSString *)path duration:(int)duration{
    TIMSoundElem *elem = [[TIMSoundElem alloc] init];
    elem.path = path;
    elem.second = duration;
    
    TIMMessage *msg = [[TIMMessage alloc] init];
    [msg addElem:elem];
    
    return [[YHChatMessage alloc] initWithMsg:msg messageType:YHChatMessageType_Sound];
}

+ (instancetype)msgWithEmptySound{
    TIMSoundElem *elem = [[TIMSoundElem alloc] init];
    
    TIMMessage *msg = [[TIMMessage alloc] init];
    [msg addElem:elem];
    
    return [[YHChatMessage alloc] initWithMsg:msg messageType:YHChatMessageType_Sound];
}





- (BOOL)isMineMsg{
    return [self.sdk_message isSelf];
}

- (BOOL)isC2CMsg{
    return [[self.sdk_message getConversation] getType] == TIM_C2C;
}

- (BOOL)isGroupMsg{
    return [[self.sdk_message getConversation] getType] == TIM_GROUP;
}

- (BOOL)isMultiMsg{
    return self.messageType == YHChatMessageType_Face || self.sdk_message.elemCount > 1;
}




























@end
